﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace GZ.Entity
{
    /// <summary>
    /// 配送区域实体类
    /// </summary>
    public class EDeliveryArea
    {
        public EDeliveryArea()
        {
            this.defaultcompanyname = string.Empty;
            this.deliveryareaname = string.Empty;
            this.parentid = 0;
        }

        #region 属性
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string deliveryareaname;

        public string DeliveryAreaName
        {
            get { return deliveryareaname; }
            set { deliveryareaname = value; }
        }
        private int parentid;

        public int ParentId
        {
            get { return parentid; }
            set { parentid = value; }
        }
        private string defaultcompanyname;

        public string DefaultCompanyName
        {
            get { return defaultcompanyname; }
            set { defaultcompanyname = value; }
        }

        private int ordervalue;

        public int OrderValue
        {
            get { return ordervalue; }
            set { ordervalue = value; }
        }
        #endregion

        #region

        /// <summary>
        /// 根据Id集合查询配送区域集合
        /// </summary>
        /// <param name="Ids">Id集合 eg.(1,2,3)</param>
        /// <remarks>
        /// 根据区域Id集合返回区域集合列表，可以将其一次性查出需要的区域。
        /// 省略多余的查询。
        /// </remarks>
        /// <returns>配送区域列表</returns>
        public DataTable GetDeliveryAreaByIds(string Ids)
        {
            string sql = "SELECT * FROM  tb_deliveryarea WHERE Id IN(" + Ids + ") ORDER BY OrderValue";
            DataTable dt = MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
            return dt;
        }

        /// <summary>
        /// 根据id删除配送地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Del(int Id)
        {
            string sql = "DELETE FROM tb_deliveryarea WHERE Id=@Id";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@Id", Id);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

       
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Edit(EDeliveryArea obj)
        {
            string sql = @" UPDATE  tb_deliveryarea
                            SET 
                            DeliveryAreaName = @DeliveryAreaName,
                            ParentId = @ParentId,
                            DefaultCompanyName = @DefaultCompanyName,
                            OrderValue = @OrderValue
                            WHERE Id=@Id
                            ";
            MySqlParameter[] parameters = new MySqlParameter[5];
            parameters[0] = new MySqlParameter("@DeliveryAreaName", obj.DeliveryAreaName);
            parameters[1] = new MySqlParameter("@ParentId", obj.ParentId);
            parameters[2] = new MySqlParameter("@DefaultCompanyName", obj.DefaultCompanyName);
            parameters[3] = new MySqlParameter("@OrderValue", obj.OrderValue);
            parameters[4] = new MySqlParameter("@Id", obj.Id);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        /// <summary>
        /// 取所有地区
        /// </summary>
        /// <returns></returns>
        public DataTable GetDeliveryArea()
        {
            if (GZ.Common.DataCacheHelper.GetCache("DeliveryArea") == null)
            {
                string sql = "SELECT * FROM  tb_deliveryarea ";
                sql += " ORDER BY OrderValue";
                DataTable dt = MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
                GZ.Common.DataCacheHelper.SetCache("DeliveryArea", dt, 60);
                return dt;
            }
            else
            {
                return (DataTable)GZ.Common.DataCacheHelper.GetCache("DeliveryArea");
            }
        }

        /// <summary>
        /// 获取所有配送区域集合
        /// </summary> 
        /// <returns></returns>
        public DataTable GetDeliveryArea(int ParentId)
        {
            //if (GZ.Common.DataCacheHelper.GetCache("DeliveryArea") == null)
            //{
            //    DataTable dt = GetDeliveryArea();
            //    IEnumerable<DataRow> q1 = from r in dt.AsEnumerable()
            //                              where r.Field<int>("ParentId") == ParentId
            //                              select r;
            //    return q1.CopyToDataTable<DataRow>();
            //}
            //else
            //{
            //    return (DataTable)GZ.Common.DataCacheHelper.GetCache("DeliveryArea");
            //}
            string sql = "SELECT * FROM  tb_deliveryarea WHERE 1=1 AND ParentId=" + ParentId;
            sql += " ORDER BY OrderValue";
            DataTable dt = MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
            return dt;
        }


        /// <summary>
        /// 修改配送区域
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool ModifyDeliveryArea(EDeliveryArea obj)
        {
            string sql = @"UPDATE  tb_deliveryarea
                           SET
                           DeliveryAreaName =@DeliveryAreaName,
                           ParentId = @ParentId,
                           DefaultCompanyName = @DefaultCompanyName,
                           OrderValue = @OrderValue
                           WHERE Id=@Id";
            MySqlParameter[] parameters = new MySqlParameter[5];
            parameters[0] = new MySqlParameter("@DeliveryAreaName", obj.DeliveryAreaName);
            parameters[1] = new MySqlParameter("@ParentId", obj.ParentId);
            parameters[2] = new MySqlParameter("@defaultcompanyname", obj.defaultcompanyname);
            parameters[3] = new MySqlParameter("@OrderValue", obj.OrderValue);
            parameters[4] = new MySqlParameter("@Id", obj.Id);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        /// <summary>
        /// 根据Id查询配送地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<EDeliveryArea> GetById(int Id)
        {
            string sql = "SELECT * FROM  tb_deliveryarea WHERE 1=1 AND Id=@Id";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@Id", Id);
            return GZ.Common.ModelConvertHelper<EDeliveryArea>.ConvertToModel(MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql, parameters).Tables[0]).ToList<EDeliveryArea>();
        }

        /// <summary>
        /// 添加配送地址
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Add(EDeliveryArea obj)
        {
            string sql = @"INSERT INTO  tb_deliveryarea
                            ( 
                            DeliveryAreaName,
                            ParentId,
                            DefaultCompanyName,
                            OrderValue)
                            VALUES
                            ( 
                            @DeliveryAreaName,
                            @ParentId,
                            @DefaultCompanyName,
                            @OrderValue);
                            ";
            MySqlParameter[] parameters = new MySqlParameter[4];
            parameters[0] = new MySqlParameter("@DeliveryAreaName", obj.DeliveryAreaName);
            parameters[1] = new MySqlParameter("@ParentId", obj.ParentId);
            parameters[2] = new MySqlParameter("@DefaultCompanyName", obj.DefaultCompanyName);
            parameters[3] = new MySqlParameter("@OrderValue", obj.OrderValue);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        #endregion
    }
}
